package cn.wizzer.app.web.modules.controllers.platform.sys;

import cn.wizzer.app.sys.modules.models.SysPayWayBank;
import cn.wizzer.app.sys.modules.models.enums.BankUsesEnum;
import cn.wizzer.app.sys.modules.services.SysPayWayBankService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.util.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

/**
 * 支持支付的银行 后台管理
 */
@IocBean
@At("/platform/sys/payWayBank")
public class SysPayWayBankController {

    @Inject
    @Reference
    private SysPayWayBankService bankService;

    @At("/")
    @Ok("beetl:/platform/sys/payWayBank/index.html")
    @RequiresPermissions("sys.finance.payWayBank.list")
    public void index() {
    }

    /**
     * 模糊搜索
     *
     * @param searchKeyword
     * @return
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.finance.payWayBank.list")
    public Object list(@Param("searchKeyword") String searchKeyword) {
        return Result.success().addData(bankService.query(Cnd.where("country", "like", "%" + searchKeyword + "%")
                .or("bank", "like", "%" + searchKeyword + "%")
        ));
    }


    /**
     * 查询银行列表（分页）
     *
     * @param searchKeyword String 搜索框输入的内容
     * @param searchDate    String[] 时间数组
     * @param pageNumber    int
     * @param pageSize      int
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("sys.finance.payWayBank.list")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(bankService.listPage(pageNumber, pageSize, searchKeyword, searchDate));
    }

    /**
     * 银行用途：仅充值，仅代付，充值、代付
     *
     * @return
     */
    @At
    @Ok("json")
    @RequiresPermissions("sys.finance.payWayBank.list")
    public Object uses() {
        return Result.success(BankUsesEnum.toList());
    }


    /**
     * 添加银行
     *
     * @param bank
     * @return
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("sys.finance.payWayBank.add")
    @SLog(tag = "添加支持支付的银行", msg = "银行id:${args[0].bankLibId}")
    public Object add(@Param("..") SysPayWayBank bank) {
        bankService.add(bank);
        return Result.success();
    }


    /**
     * 根据id查询银行信息
     *
     * @param id
     * @return
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(@Param("id") String id) {
        return Result.success(bankService.fetchVO(id));
    }

    /**
     * 修改银行信息
     *
     * @param bank
     * @return
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("sys.finance.payWayBank.edit")
    @SLog(tag = "修改支持支付的银行", msg = "银行id:${args[0].bankLibId}")
    public Object editDo(@Param("..") SysPayWayBank bank) {
        bankService.edit(bank);
        return Result.success();
    }

    /**
     * 根据id启用银行
     *
     * @param id
     * @return
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.payWayBank.edit")
    @SLog(tag = "启用支持支付的银行", msg = "银行id:${args[0].bankLibId}")
    public Object enable(String id) {
        bankService.update(Chain.make("disabled", false), Cnd.where("id", "=", id));
        bankService.clearCache();
        return Result.success();
    }

    /**
     * 根据id禁用银行
     *
     * @param id
     * @return
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.payWayBank.edit")
    @SLog(tag = "禁用支持支付的银行", msg = "银行id:${args[0].bankLibId}")
    public Object disable(String id) {
        bankService.update(Chain.make("disabled", true), Cnd.where("id", "=", id));
        bankService.clearCache();
        return Result.success();
    }

    /**
     * 根据id删除银行
     *
     * @param id
     * @return
     */
    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("sys.finance.payWayBank.delete")
    @SLog(tag = "删除支持支付的银行", msg = "银行id:${args[0].bankLibId}")
    public Object delete(String id) {
        bankService.clearCache();
        bankService.delete(id);
        return Result.success();
    }
}
